/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 5130, n8,x2 2c3 un 5230
 * Version telefono 5.1.1
 * firmware 4,12.01
 */
package org.knolle.campanias.util;

import org.knolle.campanias.util.AbstractLotesTransfer;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.ListModel;

/**
 *
 * @author marcelo
 */
public class ListaLotesTransferHandler extends AbstractLotesTransfer {

        private javax.swing.JList source=null;
        private int[] indices;
        private int addIndex = -1;// Lugar donde se agregaron los elementos
        private int addCount = 0;//Total Items Agregados
        private final Boolean IMPRIMIR = false;
        
        @Override
        protected Object[] exportarLotes(JComponent c) {
             source = (javax.swing.JList) c;
             indices = source.getSelectedIndices();
             Object[] objetos = source.getSelectedValues();
             return   objetos;
        }

        
        @Override
        protected void importarLotes(JComponent c, Object[] datos) {
            
            javax.swing.JList target = (javax.swing.JList) c;
            javax.swing.JList.DropLocation dl = target.getDropLocation();
            Integer index = dl.getIndex();
            
            DefaultListModel targetModel =  (DefaultListModel) target.getModel();

            imprimir("Lugar del Drop segun DropLocation : "+dl.getIndex(), c, false); 
            int max = targetModel.getSize();
            if (index < 0 || index > max) {
                    index = max;
            }
            addIndex = index;
            for (int i = 0; i < datos.length; i++) {
                int idx = index++;
                targetModel.add (idx, datos[i]);
                target.addSelectionInterval(idx, idx);
            }
            addCount = (target == source) ? datos.length:0;
            imprimir("Saliendo de importLotes -> "+addCount, c, true);
        }  
       
        @Override
        protected void cleanup(JComponent c, Boolean remove) {
            
        
             imprimir("entrando en cleanUp -> ",c,false);
            if (remove && indices != null) {
                javax.swing.JList target  = (javax.swing.JList)c;
                DefaultListModel model  = (DefaultListModel) target.getModel();
                //If we are moving items around in the same list, we
                //need to adjust the indices accordingly, since those
                //after the insertion point have moved.
                imprimir("Entrando en el if de cleanup -> ",c,false);
                if (addCount > 0) {
                    for (int i = 0; i < indices.length; i++) {
                        if (indices[i] > addIndex && addIndex != -1) {
                            indices[i] += addCount;
                        }
                    }
                }
                for (int i = indices.length - 1; i >= 0; i--) {
                    model.remove(indices[i]);
                }
            }
            indices = null;
            addCount = 0;
            addIndex = -1;
            imprimir("Saliendo de cleanup-> ",c,true);
        }

        @Override
        protected void importarLotes(JComponent c, Object[] datos,Object source) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
        private void imprimir(String mensaje,JComponent origen, boolean datos){
            
            if (!IMPRIMIR) {
               return;
            }
            
            javax.swing.JList fuente = (null != origen ? (javax.swing.JList) origen: null);
            if (fuente != null){
                String nombre = "";
                if (!datos)
                    System.out.print(mensaje +nombre +  "\n");
                else {
                    ListModel datosModel =  fuente.getModel();
                    System.out.print(mensaje + nombre  +"\n");
                    for (int i=0; i< datosModel.getSize() ; i++){
                       System.out.print(datosModel.getElementAt(i)+ "\n");
                    }
                    System.out.print("\n");
                    
                }    
            }
            else { System.out.print(mensaje); }
            
        }    
    
    }